﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace Sorteo.UI.Reports
{
    public partial class ReportViewerForm : Form
    {
        ReportDataSource reportDataSource;

        public ReportViewerForm()
        {
            InitializeComponent();
        }

        public string ReportTitle
        {
            get
            {
                return Text;
            }
            set
            {
                Text = value;
            }
        }
        
        private void ShowReport(Dictionary<string, object> dataSources, string reportType, IEnumerable<ReportParameter> parameters)
        {
            reportViewer1.LocalReport.DataSources.Clear();
            foreach (var source in dataSources)
            {
                reportDataSource = new ReportDataSource(source.Key, source.Value);
                reportViewer1.LocalReport.DataSources.Add(reportDataSource);
            }
            reportViewer1.LocalReport.ReportEmbeddedResource = reportType;
            if (parameters != null)
                reportViewer1.LocalReport.SetParameters(parameters);
            reportViewer1.RefreshReport();
        }

        public void BuildReport(object data, string reportType)
        {
            BuildReport(data, reportType, null);
        }

        public void BuildReport(object data, string reportType, IEnumerable<ReportParameter> parameters)
        {
            var dataSources = new Dictionary<string, object> { { "Sorteo_BusinessEntities_BEConcursanteReport", data } };
            ShowReport(dataSources, reportType, parameters);
        }

        private void ReportViewerForm_Load(object sender, EventArgs e)
        {
            reportViewer1.RefreshReport();
        }
    }
}